﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workbench.Operations;

namespace Workbench.Presentation
{
    public abstract class ProcessWordsViewViewModelBase : TabViewModelBase
    {
        private FileSelectViewModel fileSelectViewModel;
        public FileSelectViewModel FileSelectViewModel
        {
            get
            {
                return fileSelectViewModel;
            }
            set
            {
                fileSelectViewModel = value;
                RaisePropertyChanged();
            }
        }

        private SentenceViewModel sentenceViewModel;
        public SentenceViewModel SentenceViewModel
        {
            get
            {
                return sentenceViewModel;
            }
            set
            {
                sentenceViewModel = value;
                RaisePropertyChanged();
            }
        }

        private bool hasSelectedFile;
        public bool HasSelectedFile
        {
            get { return hasSelectedFile; }
            set { hasSelectedFile = value; RaisePropertyChanged(); }
        }

        private ProcessWordsListViewModel<WordViewModelBase> wordsListViewModel;
        public ProcessWordsListViewModel<WordViewModelBase> WordsListViewModel
        {
            get
            {
                return wordsListViewModel;
            }
            set
            {
                wordsListViewModel = value;
                RaisePropertyChanged();
            }
        }

        private OperationContext operationContext;
        public OperationContext OperationContext
        {
            get { return operationContext; }
            set { operationContext = value; RaisePropertyChanged(); }
        }

        public ProcessWordsViewViewModelBase(string header) : base(header)
        {
            WordsListViewModel = new ProcessWordsListViewModel<WordViewModelBase>();
            WordsListViewModel.PropertyChanged += WordsListViewModel_PropertyChanged;
            FileSelectViewModel = new FileSelectViewModel();
            FileSelectViewModel.PropertyChanged += FileSelectViewModel_PropertyChanged;
            SentenceViewModel = new SentenceViewModel();
            OperationContext = OperationContext.Selection;
        }

        void WordsListViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (string.Compare(e.PropertyName, "LastSelectedWord", false) == 0)
            {
                var vm = sender as ProcessWordsListViewModel<WordViewModelBase>;
                if (vm.LastSelectedWord == null) return;

                SentenceViewModel.ShowSentence(vm.LastSelectedWord);
            }
        }

        void FileSelectViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (string.Compare(e.PropertyName, "SelectedFile", false) == 0)
            {
                if (FileSelectViewModel.SelectedFile != null)
                {
                    LoadFile(FileSelectViewModel.SelectedFile);
                }
            }
        }

        public abstract Task LoadFile(ProjectFileViewModel fileViewModel);
    }

    public enum OperationContext 
    {
        Selection,
        CurrentFile,
        AllFiles
    }
}
